package sopraim.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import sopraim.entity.Status;

public class StatusDAO {
    private SessionFactory factory;
    
    public StatusDAO(SessionFactory factory) {
        this.factory = factory;
    }
    
    public  void addStatus(Status s) {
        Session  session = factory.getCurrentSession();
        session.beginTransaction();
        session.save(s);
        session.getTransaction().commit();
    }
    
    
    
    public  ArrayList<String> getAllStatus() {
        Session  session = factory.getCurrentSession();
        session.beginTransaction();
        List list = session.createQuery("from Status").list();
        ArrayList<String> results = new ArrayList<String>();
        Iterator it = list.iterator();
        while(it.hasNext())
        {
            Status st = (Status)it.next();
            results.add(st.getName());
        }
        // TODO Auto-generated method stub
        return results;
    }
    
    public Status getStatus(String name) throws Exception{
        Session  session = factory.getCurrentSession();
        session.beginTransaction();
        List list = session.createQuery("from Status").list();
        Iterator it = list.iterator();
        while(it.hasNext())
        {
            Status st = (Status)it.next();
            if(st.getName().equals(name)) return st;
        }
        throw new Exception("No such status");
    }
    
}
